function loadCategories()
{
  var dom = readCategories();
  var children=dom.documentElement.childNodes;

  for(var x=0;x<children.length;x++)
  {
    if(children[x].nodeName=='Category')
      addCategoryFromXml(children[x]);
  }
}

function addCategoryFromXml(xmlNode)
{
  var children=xmlNode.childNodes;
  var title='',idx='';
  
  for(var x=0;x<children.length;x++)
  {
    if(children[x].nodeName=='Name')
      title=getXMLNodeValue(children[x]);
    if(children[x].nodeName=='Code')
      idx=getXMLNodeValue(children[x]);
  }
  
  title=unescape(title);
  addCategory(title,idx);
}

function addCategory(title,idx)
{
  //<tab label="{tab label}" id="oldone" ondragover="nsDragAndDrop.dragOver(event,tabLabelObserver);" ondragdrop="nsDragAndDrop.drop(event,tabLabelObserver);"/>
  var tabID="tab_"+idx;
  var panelID="tabpan_"+idx;
  var boxID="rtlb_"+idx;

  var nt=document.createElementNS(XUL_NS,"tab");
  nt.setAttribute("label",title);
  nt.setAttribute("id",tabID);
  nt.setAttribute("ondragover","nsDragAndDrop.dragOver(event,tabLabelObserver);");
  nt.setAttribute("ondragdrop","nsDragAndDrop.drop(event,tabLabelObserver);");
  
  var np=document.createElementNS(XUL_NS,"tabpanel");
  np.setAttribute("id",panelID);
  
  var nb=document.createElementNS(XUL_NS,"richlistbox");
  nb.setAttribute("id",boxID);
  nb.setAttribute("flex","1");
  nb.setAttribute("ondblclick","editNote('"+boxID+"');");

  np.appendChild(nb);

  document.getElementById('alltabs').appendChild(nt);    
  document.getElementById('allpanels').appendChild(np);    
}

function addNewCategory()
{
  var d=new Date();
  var cn=prompt("Please enter the name for the new category","");
  
  if(cn!=null)
  {
    cn=cn.trim();
    if(cn!='')
    {
      // add to file
      idx=d.getTime();
      createNewCategory(cn,idx);
      addCategory(cn,idx);
    }
    else
      alert('You cannot add a category without a name.');
  }
}

function createNewCategory(title,idx)
{
  var dom = readCategories();
  var titleText=dom.createTextNode(escape(title));
  var idxText=dom.createTextNode(idx);

  var e=dom.createElement("Category");

  var titleElm=dom.createElement("Name");
  titleElm.appendChild(titleText);

  var idxElm=dom.createElement("Code");
  idxElm.appendChild(idxText);
  

  e.appendChild(titleElm);
  e.appendChild(idxElm);
  dom.documentElement.appendChild(e);

  writeCategories(dom);
}

function getCurrentTabIndex()
{
  var tabsControl=document.getElementById("alltabs");
  var idx=tabsControl.selectedIndex;
  
  return(idx);  
}

function getCurrentTabCode()
{
  var idx=getCurrentTabIndex();
  var tc=document.getElementById("alltabs");
  //var tabControl=tc.getItemAtIndex(idx);
  var tabControl=tc.selectedItem;
  var tabID=tabControl.getAttribute('id');
  var rawID=rawID=tabID.substring(4);
  
  return(rawID);
}

function getCurrentRichListBoxID()
{
  var catIdx=getCurrentTabCode();
  var catSearch;
  
  if(catIdx==null)  catIdx='';
  if(catIdx=='-1')  catIdx='';
  if(catIdx=='uncat')  catIdx='';
  
  if(catIdx=='')
    catSearch='thundernotelist2';
  else
    catSearch='rtlb_'+catIdx;
    
  return(catSearch);
}

function deleteSelectedCategory()
{
  var tabsControl=document.getElementById("alltabs");
  var tabPanelControl=document.getElementById("allpanels");
  var pan;
  var richListBoxControl;
  var idx=tabsControl.selectedIndex;
  var tempNode;
  var tabControl;
  var tabID;          //tab_
  var panelID;        //tabpan_
  var richListBoxID;  //rtlb_
  var rawID;
  var cnt;
  
  if(idx>0)
  {
    //tabControl=tabsControl.getItemAtIndex(idx);
    tabControl=tabsControl.selectedItem;
    tabID=tabControl.getAttribute('id');
    rawID=tabID.substring(4);
    panelID="tabpan_"+rawID;
    richListBoxID="rtlb_"+rawID;
    richListBoxControl=document.getElementById(richListBoxID);
    
    pan=document.getElementById(panelID);
    cnt=richListBoxControl.getRowCount();
    
    if(cnt<1)
    {
      if(confirm('Are you sure you want to delete the selected Category?'))
      {
        removeCategory(rawID);
        //alertXML(tabsControl);
        tabPanelControl.removeChild(pan);
        tabsControl.removeItemAt(idx);
        //tabsControl.removeChild(tabControl);
        //tabsControl.selectedIndex=-;
        tabsControl.selectedIndex=0;
        //alertXML(tabsControl);
      }
    }
    else
      alert('You cannot delete a Category which still has ThunderNotes attached');
  }
  else
    alert('You cannot delete the \"Uncategorized\" Category');
}

function removeCategory(idx)
{
  var dom = readCategories();
  var found=false;
  
  var children=dom.documentElement.childNodes;
  for(var x=0;x<children.length;x++)
  {
    if(children[x].nodeName=='Category')
    {
      if(checkNodeIndex(children[x],idx,'Code'))
      {
        dom.documentElement.removeChild(children[x]);
        found=true;      
        break;
      }
    }
  }
  
  if(found)
  {
    writeCategories(dom);
  }  
}